package com.fs.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.fs.dao.OderDao;
import com.fs.entity.Oder;
@WebServlet("/address.do")
public class OderServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			PrintWriter out = resp.getWriter();
			String type=req.getParameter("type");
			HttpSession session=req.getSession();
			OderDao oderDao=new OderDao();
			Oder oder=requestGetDate(req);
			JSONObject json=new JSONObject();
			int oder_id=0;
			oder.setoder_id(oder_id);
			if("add".equals(type)){
				oder_id = oderDao.add(oder);//执行添加收货，执行需要返回列表（这个时候需要执行查询操作）
				session.setAttribute("oder_id",oder_id);
				List<Oder> oderlist=OderDao.getOderList(oder_id);//这个集合需要返到页面
				JSONArray jsonarray = null;
				try {
					jsonarray=JSONArray.fromObject(oderlist);
					json.put("oderlist",jsonarray );
					json.put("code", 0);
					json.put("message", "添加成功");
					System.out.println(jsonarray.toString());
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.print(jsonarray);
				
			}else if("query".equals(type)){
				int order_id = 0;
				if(session.getAttribute("oder_id")!=null){
					order_id=(Integer)session.getAttribute("oder_id");
				}
				oder=OderDao.getOderById(order_id);//这个集合需要返到页面
				JSONArray jsonarray=JSONArray.fromObject(oder);
				System.out.println(oder);
				json.put("oder",jsonarray );
				json.put("code", 0);
				json.put("message", "查询成功");
				out.println(json.toString());
			}else if("querylist".equals(type)){
				System.out.println("---------------");
				int user_id = 0;
				if(session.getAttribute("user_id")!=null){
					user_id=(Integer)session.getAttribute("user_id");
				}
				List<Oder> oderlist=OderDao.getOderList(user_id);//这个集合需要返到页面
				JSONArray jsonarray=JSONArray.fromObject(oderlist);
				json.put("oderlist",jsonarray );
				json.put("code", 0);
				json.put("message", "查询成功");
				out.println(json.toString());
			}
			
		
	}
	private Oder requestGetDate(HttpServletRequest req) {
		HttpSession session=req.getSession();
		//获取数据的封装
		String oder_price=req.getParameter("oder_price");//订单价格
		String receipt_phone=req.getParameter("receipt_phone");//收货人电话
		String receipt_addr=req.getParameter("receipt_addr");//收货人地址
		String receipt_name=req.getParameter("receipt_name");//收货人姓名
		Oder Oder=new Oder(oder_price,receipt_name,receipt_phone, receipt_addr);
	
		return Oder;
	
	}
	

}
